﻿using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RuleOfReady.Core.DomainModels.Tiles;
using RuleOfReady.Core.DomainModels.Sets;
using RuleOfReady.Core.Engines;
using RuleOfReady.Core.Test.Util;

namespace RuleOfReady.Core.Test.DomainModelTest
{
    [TestClass]
    public class TileEngineTests
    {
        TileEngine tileEngine;

        [TestInitialize]
        public void Setup()
        {
            tileEngine = new TileEngine();
        }

        [TestMethod]
        public void TileSetCreatedProperly()
        {
            IList<MahjongTile> tileSet = this.tileEngine.CreateGameTileSet();

            Assert.IsTrue(tileSet.Count == 136);

            HashSet<MahjongTile> tileTypes = new HashSet<MahjongTile>();
            TileTestUtil.AddEveryTile(tileTypes);

            // 4 of every suit
            foreach (MahjongTile tile in tileTypes)
            {
                Assert.IsTrue(tileSet.Where(t => t == tile).Count() == 4);
            }

            // 4 red tiles if used
            IList<MahjongTile> redTileSet = this.tileEngine.CreateGameTileSet(useRedBonusTiles: true);
            Assert.IsTrue(redTileSet.OfType<MahjongSuitTile>().Where(tile => tile.IsRedBonus).Count() == 4);
        }

        [TestMethod]
        public void TileSequencesValidation()
        {
            var dotSequences = this.tileEngine.SequencesBySuit[MahjongSuitType.Dot];
            Assert.IsTrue(dotSequences.Count() == 7);

            MahjongSequence[] sequenceArray = dotSequences.ToArray();
            for(int i=0; i < 6; i++)
            {
                MahjongSuitTile SeqTile = sequenceArray[i].Cast<MahjongSuitTile>().First();
                MahjongSuitTile nextSeqTile = sequenceArray[i + 1].Cast<MahjongSuitTile>().First();
                Assert.IsTrue(SeqTile.SuitNumberInt + 1 == nextSeqTile.SuitNumberInt);
            }

            foreach (var sequence in sequenceArray)
            {
                var tileArray = sequence.Cast<MahjongSuitTile>().ToArray();
                Assert.IsTrue(tileArray[0].SuitNumberInt + 1 == tileArray[1].SuitNumberInt);
                Assert.IsTrue(tileArray[1].SuitNumberInt + 1 == tileArray[2].SuitNumberInt);
            }
        }
    }
}
